package com.rajaev.dtu.tractor.domain.repository.mysql;

import com.rajaev.dtu.tractor.domain.vo.CustomerVo;
import com.rajaev.dtu.tractor.domain.entity.CustomerPo;
import com.rajaev.dtu.tractor.interfaces.dto.CustomerWithProjectTeamVo;
import org.apache.ibatis.annotations.*;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
@CacheConfig(cacheNames = "customers")
public interface CustomerDao {

    /**
     * 新增一条客户信息
     * @param customerPo
     * @return
     */
    Integer saveCustomer(CustomerPo customerPo);


    CustomerPo selectCustomerByLicenceExceptId(@Param("licence") String licence,@Param("id") Integer id);


    List<CustomerWithProjectTeamVo> selectCustomerVoByUserId(@Param("userId") Integer userId);



    //@Cacheable(key = "#p0")
    @Select("select * from t_customer where customer_name like concat('%',#{name},'%') and delete_flag = '0' ")
    @ResultMap("CustomerVoMap")
    List<CustomerVo> selectCustomerByName(@Param("name") String name);
    /**
     * 根据客户名称查看客户
     * @param customerName
     * @return
     */
    @Select({
            "<script>",
            "select  * from t_customer where customer_name = #{customerName}",
            "</script>",
    })
    @ResultMap("CustomerPoMap")
    CustomerPo selectByCustomerName(String customerName);

    /**
     * 根据客户id查看客户是否存在
     * @param customerId
     * @return
     */
    @Select({
            "<script>",
            "select  * from t_customer where id = #{customerId}",
            "</script>",
    })
    @ResultMap("CustomerPoMap")
    CustomerPo selectByCustomerId(Integer customerId);


    /**
     * 修改客户信息
     * @param customerPo
     * @return
     */
    @CacheEvict(key = "#p0.customerName",allEntries = true)
    Integer updateCustomer(CustomerPo customerPo);


    @Select({
            "<script>",
            "select  * from t_customer where delete_flag ='0' ",
            "</script>",
    })
    @ResultMap("CustomerVoMap")
    List<CustomerVo> selectCustomers();

    /** @Cacheable(key = "#p0")**/
    List<CustomerVo> selectCustomersByUserId(Integer userId);

    /**
     * 逻辑删除一个客户
     * @param customerId
     */
    @Update({
            "update t_customer set delete_flag = '1' where id = #{customerId} "
    })
    //@CacheEvict(allEntries = true)
    Integer deleteCustomer(Integer customerId);

}
